import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: { // 存储数据
    count: 0,
    token: '1234567890'
  },
  mutations: { // 变更数据
    plusCount(state) {
      state.count++;
    },
    reduceCount(state) {
      state.count--;
    },
    reduceCountN(state, n) {
      state.count -= n;
    },
    plusCountN(state, n) {
      state.count += n;
    }
  },
  actions: { // 异步变更数据
    reduceCountAsync(context) {
      setTimeout(() => {
        context.commit('reduceCount');
      }, 1000);
    },
    reduceCountAsyncN(context, n) {
      setTimeout(() => {
        context.commit('reduceCountN', n);
      }, 1000);
    }
  },
  getters: { // 数据监听
    currentCount(state) {
      return `当前getters为：${state.count}`;
    }
  }
});
